package com.itheima;

import com.bean.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
@WebServlet("/addStudent")
public class AddStudent extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       req.setCharacterEncoding("UTF-8");
        String id = req.getParameter("id");
        String age = req.getParameter("age");
        String score = req.getParameter("score");
        Student stu=new Student(id,age,score);
        String realPath = this.getServletContext().getRealPath("/b.txt");
        BufferedWriter bw=new BufferedWriter(new FileWriter(realPath,true));
        bw.write(stu.getId()+","+stu.getAge()+","+stu.getScore());
        bw.close();
        resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().write("添加成功！2秒后跳回首页");
        resp.setHeader("Refresh","2;URL=index.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
